package cqie.frame.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cqie.frame.core.PageParam;
import cqie.frame.entity.Clazz;
import cqie.frame.entity.EvaluateTask;
import cqie.frame.entity.EvaluateType;
import cqie.frame.entity.qo.EvaluateTaskQo;
import cqie.frame.entity.vo.EvaluateTaskVo;
import cqie.frame.response.CodeMsg;
import cqie.frame.response.ResultJson;
import cqie.frame.service.IEvaluateTaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 评教任务(EvaluateTask)表控制层
 *
 * @author makejava
 * @since 2021-10-09 11:04:25
 */
@RestController
@RequestMapping("/evaluateTask")
public class EvaluateTaskController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IEvaluateTaskService iEvaluateTaskService;



    //新增新增评价类型
    @ApiOperation(value = "新增评价类型", notes = "新增评价类型，同时更新评价类型表。", response = EvaluateType.class)
    @PostMapping(value = "/add")
    public ResultJson add(@RequestBody EvaluateTask po) {
        try {
            //构造mo
            EvaluateTask evaluateTask = new EvaluateTask();
            //复制po的属性到evaluateType,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, evaluateTask);
            //插入用户表后回显插入课程的主键id
            iEvaluateTaskService.save(evaluateTask);
            po.setId(evaluateTask.getId());
            //返回评价类型
            return ResultJson.success(po);
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }
    //删除选中评价类型，多选
    @ApiOperation(value = "删除选中评价类型", notes = "删除选中评价类型。")
    @PostMapping(value = "/deleteCheckEvaluateTask")
    public ResultJson deleteCheckEvaluateTask(@RequestBody Long[] evaluateTaskIdList) {
        try {
            for (int i = 0; i < evaluateTaskIdList.length; i++) {
                iEvaluateTaskService.removeById(evaluateTaskIdList[i]);
            }
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除评价类型
    @ApiOperation(value = "删除评价类型", notes = "删除评价类型。")
    @GetMapping(value = "/deleteEvaluateTask")
    public ResultJson deleteEvaluateTask(long id) {
        try {
            //删除评价类型
            iEvaluateTaskService.removeById(id);
            //返回前端
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }


    //修改评价类型表
    @ApiOperation(value = "修改评价类型表", notes = "修改评价类型表，同时更新评价类型表。")
    @PostMapping(value = "/updateEvaluateTask")
    public ResultJson updateEvaluateTask(@RequestBody EvaluateTask po) {
        try {
            //获取修改前的值
            EvaluateTask evaluateTask = new EvaluateTask();
            //复制po的属性到evaluateTask,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, evaluateTask);
            //修改评价任务
            iEvaluateTaskService.updateById(evaluateTask);
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "分页列表", response = EvaluateTaskVo.class)
    @PostMapping(value = "/listPaged")
    public ResultJson listPaged(@RequestBody EvaluateTaskQo qo) {
        try {
            Page<EvaluateTask> page = new Page<>(qo.getPageIndex(), qo.getPageSize());
            //1.查询条件
            QueryWrapper<EvaluateTask> qw = new QueryWrapper<>();
            if (!StringUtils.isEmpty(qo.getName())) {
                //查询模糊查询
                qw.like("name", qo.getName());
            }
            //2.执行查询
            IPage<EvaluateTask> datas = iEvaluateTaskService.page(page, qw);
            //3.生成返回值
            //映射
            List<EvaluateTaskVo> list = new ArrayList<>();
            IPage<EvaluateTaskVo> ret = new Page<>();
            BeanUtils.copyProperties(datas, ret);
            for (EvaluateTask mo : datas.getRecords()) {
                EvaluateTaskVo vo = new EvaluateTaskVo();
                BeanUtils.copyProperties(mo, vo);
                //映射--状态
                vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
                list.add(vo);
            }
            ret.setRecords(list);
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return  ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "查看详情", response = EvaluateTaskVo.class)
    @GetMapping(value = "/details")
    public ResultJson details(Long id) {
        try {
            //1.获取mo
            EvaluateTask mo = iEvaluateTaskService.getById(id);
            //2.映射成vo
            EvaluateTaskVo vo = new EvaluateTaskVo();
            BeanUtils.copyProperties(mo, vo);
            //映射准备
            //映射--状态
            vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
            //3.返回
            return  ResultJson.success(vo);
        } catch (Exception ex) {
            return  ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复选中任务，多选
    @ApiOperation(value = "注销/恢复选中任务", notes = "注销/恢复选中班级，同时更新班级表。")
    @PostMapping(value = "/disabledCheckTask")
    public ResultJson disabledCheckTask(@RequestBody Long[] evaluateTaskIdList) {
        try {
            for (int i = 0; i < evaluateTaskIdList.length; i++) {
                EvaluateTask evaluateTask = new EvaluateTask();
                evaluateTask = iEvaluateTaskService.getById(evaluateTaskIdList[i]);
                if (evaluateTask.getStatus() == 0) {
                    //如果修改前为正常，修改为禁用
                    evaluateTask.setStatus(1);
                } else if (evaluateTask.getStatus() == 1) {
                    //如果修改前为禁用，修改为正常
                    evaluateTask.setStatus(0);
                }
                iEvaluateTaskService.updateById(evaluateTask);
            }
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复班级
    @ApiOperation(value = "注销/恢复单个班级", notes = "注销/恢复单个班级，同时更新班级表。")
    @GetMapping(value = "/disabledTask")
    public ResultJson disabledTask(long id) {
        try {
            EvaluateTask evaluateTask = new EvaluateTask();
            evaluateTask = iEvaluateTaskService.getById(id);
            if (evaluateTask.getStatus() == 0) {
                //如果状态为正常，修改为禁用
                evaluateTask.setStatus(1);
            } else if (evaluateTask.getStatus() == 1) {
                //如果状态为禁用，修改为正常
                evaluateTask.setStatus(0);
            }
            //修改
            iEvaluateTaskService.updateById(evaluateTask);
            return ResultJson.success("操作成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }
}

